package com.mrjy.common.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mrjy.common.entity.DistributorInfo;
import com.mrjy.common.entity.DistributorTeamInfo;
import com.mrjy.share.dao.IDistributorInfoDao;
import com.mrjy.share.dao.IDistributorTeamInfoDao;
import com.mrjy.share.dto.DistributorInfoDTO;

/**
 * 用于处理禁用/启用 讲师,服务站,分销商,合作方时 其下属团队成员状态的变更
 * @author Administrator
 *
 */
public class UpdateDisableFlagUtil {
	private static IDistributorInfoDao distributorInfoDao;
	private static IDistributorTeamInfoDao distributorTeamInfoDao;

	public static void updateDisableFlagForTeam(String[] distributorTypes,Integer userId,Integer disableFlag){
        if (distributorInfoDao == null) {
            distributorInfoDao = (IDistributorInfoDao) SpringContextHelper.getBean(IDistributorInfoDao.class);
        }
        if (distributorTeamInfoDao == null) {
            distributorTeamInfoDao = (IDistributorTeamInfoDao) SpringContextHelper.getBean(IDistributorTeamInfoDao.class);
        }

		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("userId", userId);
		paramMap.put("distributorTypes", distributorTypes);
//		if(disableFlag.intValue() == 0 ){
//			paramMap.put("disabledFlag", 1);
//		}else if(disableFlag.intValue() == 1 ){
//			paramMap.put("disabledFlag", 0);
//		}
		//根据userId和distributorType查询推广身份
		List<DistributorInfoDTO> diList = distributorInfoDao.queryByMap(paramMap) ;
		if(diList!= null && diList.size() > 0 && diList.get(0).getId() != null){
			DistributorInfo di = new DistributorInfo();
			di.setId(diList.get(0).getId());
			di.setDisabledFlag(disableFlag);
			int count = distributorInfoDao.updateById(di);
			if(count > 0){
				//判断操作是启用还是禁用
				//如果是禁用,则需要查询除要禁用的身份以外 是否还有其他(非普通推广者且启用)身份
				//如果还有其他(非普通推广者且启用)身份, 则不作废其下属团队成员
				//操作分销队员 
				List<DistributorTeamInfo> teamList = distributorTeamInfoDao.queryByBelongTeamId(userId);
				if (disableFlag.intValue() == 1) {
					DistributorInfo valid = distributorInfoDao.queryValidPomotorByUserId(userId);
					if (valid == null) {
						operateTeamMember(teamList, disableFlag);
					}
				}else {
					operateTeamMember(teamList, disableFlag);
				}
			}
		}
	}
	
	private static void operateTeamMember(List<DistributorTeamInfo> teamList, Integer disableFlag) {
		String[] _distributorTypes = {"team"};
		DistributorInfo vo = new DistributorInfo();
		Map<String, Object> map = new HashMap<>();
		int[] ids = null;
		if(teamList != null && teamList.size() > 0){
			for(DistributorTeamInfo team : teamList){
				team.setDisableFlag(disableFlag);
				distributorTeamInfoDao.updateById(team);
				
				//禁用/启用 团队成员身份(自定义分成比例那条)
				map.put("userId", team.getUserId());
				map.put("distributorTypes",_distributorTypes);
				map.put("disableFlag",0);
				List<DistributorInfoDTO> dList = distributorInfoDao.queryByMap(map);
				ids = new int[dList.size()];
				for (int i=0;i<dList.size();i++) {
					ids[i] = dList.get(i).getId().intValue();
				}
			}
			vo.setIds(ids);
			vo.setDisabledFlag(disableFlag);
			distributorInfoDao.updateDistributors(vo);
		}
	}
	
}
